function createStore(reducer) {
  let state;
  const listeners = [];

  const getState = () => {
    return state;
  };

  const dispatch = (action) => {
    state = reducer(state, action);
    listeners.forEach((l) => l());
  };

  const subscribe = (listener) => {
    listeners.push(listener);

    return () => {
      const index = listeners.indexOf(listener);
      listeners.splice(index, 1);
    };
  };

  dispatch({ type: "@@REDUX/INIT" });

  return {
    subscribe,
    getState,
    dispatch,
  };
}

export default createStore;
